$(window).load(function(){
	/* 监听input，改变绑定按钮颜色 */
	$(".phoneNum").on("input propertychange", function(){
		changeNextColor();
	})

	$(".codeNum").on("input propertychange", function(){
		changeNextColor();
	})

	function changeNextColor(){
		if($(".phoneNum").val() != "" && $(".codeNum").val() != ""){
			$(".next p").css({"background-color": "#EA4040"});
		}else{
			$(".next p").css({"background-color": "#D4D3D3"});
		}
		
	}



	/* 初次获取验证码 */
	$(".getcode").on("click", function(){
		getCode($(".phoneNum").val());
	})



	/* 获取验证码,验证码倒计时 */
	var getCodeUrl = wxAddress + "/verify_code/send";
	var oldPhone;
	function getCode(phoneNum){
		var getCodeData = {
			mobile: phoneNum
		}
		if($(".phoneNum").val() == ""){
			warnText($(".red-bg"),"手机号不能为空！",1000);
			return false;
		}else if(regs.phoneReg.test($(".phoneNum").val()) == false){
			warnText($(".red-bg"),"手机号格式错误！",1000);
			return false;
		}
		$(".getcode").off("click");
		var seconds = 59;
		var timer = setInterval(function(){
			if(seconds == 0){
				$(".getcode").html("重新获取");
				clearInterval(timer);
				$(".getcode").on("click", function(){
					getCode($(".phoneNum").val());
				})
			}else{
				$(".getcode").html(seconds+"s");
				seconds--;
			}
		},1000);
		oldPhone = $(".phoneNum").val();
		ajaxEncapsulation(getCodeUrl, getCodeSuc, "", "POST", "true", getCodeData);
	}

	var sessionCode;
	function getCodeSuc(data){
		console.log(data);
		if(data.code = "0000"){
			sessionCode = data.data;
		}else{
			warnText($(".red-bg"),"获取验证码失败！",1000);
		}
	}



	/* 绑定按钮 */
	var bindUrl = wxAddress + "/weixin/login";
	$(".next p").on("click", function(){
		if(sessionCode){
			if($(".phoneNum").val() == ""){
				warnText($(".red-bg"),"手机号不能为空！",1000);
				return false;
			}else if(regs.phoneReg.test($(".phoneNum").val()) == false){
				warnText($(".red-bg"),"手机号格式错误！",1000);
				return false;
			}else if($(".phoneNum").val() != oldPhone){
				warnText($(".red-bg"),"已更换手机，请重新获取验证码！",1000);
				return false;
			}
			if($(".codeNum").val() == ""){
				warnText($(".red-bg"),"验证码不能为空！",1000);
				return false;
			}else if(regs.phoneCodeReg.test($(".codeNum").val()) == false){
				warnText($(".red-bg"),"验证码格式错误！",1000);
				return false;
			}else if($(".codeNum").val() != sessionCode){
				warnText($(".red-bg"),"验证码不匹配！",1000);
				return false;
			}
			$(".masklayer").show();
			$(".maskImg").show();
			var bindData = {
				phone: $(".phoneNum").val(),
				token: $(".codeNum").val()
			}
			ajaxEncapsulation(bindUrl, bindUrlSuc, "", "POST", "true", bindData);
		}else{
			warnText($(".red-bg"),"请先获取验证码！",1000);
		}
	})

	function bindUrlSuc(data){
		$(".masklayer").hide();
		$(".maskImg").hide();
		if(data.code = "0000"){
			warnText($(".red-bg"),"绑定成功！",1000);
			setTimeout(function(){
				window.location.href = "../eBiz/bizhome.html";
			},1000);
		}else{
			warnText($(".red-bg"),"绑定失败！",1000);
		}
	}
})